package com.kou.filter;

import com.alibaba.fastjson.JSONObject;
import com.kou.pojo.Type;
import com.kou.pojo.User;
import com.kou.service.GoodsTypeService;
import com.kou.service.impl.AdminServiceImple;
import com.kou.service.impl.GoodsTypeServiceImpl;
import com.kou.utils.Base64Utils;
import com.kou.utils.Constants;
import com.kou.utils.StrUtils;
import com.qf.service.AdminService;

import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * @author SteinY
 * @create 2022-03-25 10:18
 * @description:
 */
@WebFilter({"/admin/showGoodsType.jsp","/admin/addGoodsType.jsp","/admin/updateGoodsType.jsp"})
public class GoodsTypeFilter implements Filter {
    GoodsTypeService goodsTypeService = new GoodsTypeServiceImpl();
    @Override
    public void init(FilterConfig filterConfig) throws ServletException {

    }

    //从cookie中获取两周之内的用户信息
    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
        HttpServletRequest request = (HttpServletRequest) servletRequest;
        HttpServletResponse response = (HttpServletResponse) servletResponse;
        List<Type> types = goodsTypeService.findTypes();
        for (Type type : types) {
            String parentName = goodsTypeService.findParentName(type.getParent());
            if (!StrUtils.empty(parentName)){
                parentName = "无";
            }
            type.setParentName(parentName);
        }
       // System.out.println(types.size());
        request.getSession().setAttribute("goodsTypeList",types);
        //放行！！
        filterChain.doFilter(request,response);
    }

    @Override
    public void destroy() {

    }
}